package interfaz2;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;

/**
 * Abstract class to create composites used in Dust that hides a bit of
 * functionality like show or hide them.
 * 
 * If you plan to create a new composite for Dust you should extend this class.
 *  
 * @author ole
 */
public abstract class DustComposite extends Composite {
	/**
	 * Class constructor. It fills the layout.
	 * 
	 * @param parent
	 * 		The parent composite of this new composite.
	 * @param style
	 * 		The style of the composite.
	 */
	public DustComposite(Composite parent, int style) {
		super(parent, style);
		setLayout(new GridLayout());
		setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
	}
	
	/**
	 * Method that tells the index of a Control inside this DustComposite.
	 * 
	 * @param control:
	 * 		The Control we are searching its index.
	 * @return:
	 * 		The index of the Control inside this DustComposite or -1 if it
	 * 		isn't inside.
	 */
	public int getIndexOf(Control control) {
		Control child;
		
		// Loop through the children searching for this control. If we find it
		// return its index.
		for (int i = 0; i < getChildren().length; i++) {
			child = getChildren()[i];
			if (child.equals(control))
				return i;
		}
		
		return -1;
	}
	
	/**
	 * Method that hides the composite.
	 */
	public void hide() {
		GridData grid = (GridData)this.getLayoutData();
		grid.exclude = true;
		this.setVisible(false);
	}
	
	/**
	 * Method that shows the composite. Calling this method doesn't implies
	 * the composite will be shown, it depends on its parents properties.
	 */
	public void show() {
		GridData grid = (GridData)this.getLayoutData();
		grid.exclude = false;
		this.setVisible(true);
	}
}
